'''
Created on Nov 8, 2009

@author: mkiyer
'''
import numpy as np

def copa_transform(covs):    
    median = np.median(covs)
    mad = np.median(np.abs(covs - median))
    if mad == 0.0:
        return np.zeros(len(covs), dtype=float)
    return (covs - median) / mad

def calc_copa_score(covs, r=0.9):
    covs = np.log(covs)
    copa_scores = copa_transform(covs)
    sorted_copa_scores = sorted(copa_scores)
    index = int(round(r * len(covs)) - 1)
    return sorted_copa_scores[index]

